package com.sgb.link.view.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.DrawableRes;

import com.sgb.link.R;
import com.swgk.core.util.DisplayUtil;

public class ActionBarRed extends RelativeLayout implements TextView.OnEditorActionListener {

    ImageView iconFontBack;
    TextView tvTitle;
    TextView fontIconLdrtl;
    private ImageView fivContactSearch;
    private EditText editSearch;
    private ClearEdiText clearEdiText;
    private ImageView ivClose;
    private RelativeLayout relativeLayout;
    private View view;

    public ClearEdiText getClearEdiText() {
        return clearEdiText;
    }

    public void setClearEdiText(ClearEdiText clearEdiText) {
        this.clearEdiText = clearEdiText;
    }

    @Override
    public void setVisibility(int visibility) {
        super.setVisibility(visibility);
    }

    public ActionBarRed(Context context) {
        super(context);
        init(context, null);
    }

    public ActionBarRed(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public ActionBarRed(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        if (attrs != null) {
            TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.ActionBarYoung, 0, 0);
            try {
                LayoutInflater.from(context).inflate(R.layout.action_bar_red_enterprise, this);

                //回退按钮
                iconFontBack = findViewById(R.id.iv_back_red);
                //actionbar的标题
                tvTitle = findViewById(R.id.title_view_red);
                fivContactSearch = findViewById(R.id.map_view_red);
                //右边的文字
                fontIconLdrtl = findViewById(R.id.right_view_red);
                int txtColor = attributes.getColor(R.styleable.ActionBarYoung_txtColor, 0);
                tvTitle.setTextColor(txtColor);

                String font = attributes.getString(R.styleable.ActionBarYoung_title);
                tvTitle.setText(font);
                tvTitle.setTextColor(txtColor);

                boolean isVisibleMenu = attributes.getBoolean(R.styleable.ActionBarYoung_isVisibleMenu, false);
                if (isVisibleMenu) {
                    fivContactSearch.setVisibility(VISIBLE);
                } else {
                    fivContactSearch.setVisibility(GONE);
                }

                String menuFont = attributes.getString(R.styleable.ActionBarYoung_menuText);
                if (menuFont == null || menuFont.equals("")) {
                    menuFont = "保存";
                }
                fontIconLdrtl.setText(menuFont);
                fontIconLdrtl.setTextColor(txtColor);


                editSearch = findViewById(R.id.edit_search_red);
                ivClose = findViewById(R.id.iv_close_red);
                boolean isVisibleEdit = attributes.getBoolean(R.styleable.ActionBarYoung_isVisibleEdit, false);
                setEditVisible(isVisibleEdit);

                String editHint = attributes.getString(R.styleable.ActionBarYoung_editHint);
                if (editHint != null && !editHint.equals("")) {
                    editSearch.setHint(editHint);
                }

                ivClose.setVisibility(GONE);
                editSearch.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {

                    }

                    @Override
                    public void afterTextChanged(Editable s) {
                        if (editSearch.getText().toString().equals("")) {
                            ivClose.setVisibility(GONE);
                        } else {
                            ivClose.setVisibility(VISIBLE);
                        }
                    }
                });

                ivClose.setOnClickListener(v -> {
                    editSearch.setText("");
                    if (clearEdiText != null) {
                        clearEdiText.clearEdiText();
                    }
                });
                int textSize = (int) attributes.getDimension(R.styleable.ActionBarYoung_menuTextSize, 0);
                if (textSize != 0) {
                    fontIconLdrtl.setTextSize(DisplayUtil.px2sp(getContext(), textSize));
                }

                boolean isVisibleMenuFunction = attributes.getBoolean(R.styleable.ActionBarYoung_isVisibleMenuFunction, false);
                setMenuFunctionVisible(isVisibleMenuFunction);

                boolean titleVisible = attributes.getBoolean(R.styleable.ActionBarYoung_titleVisible, false);
                setTitleVisible(titleVisible);

                editSearch.setOnEditorActionListener(this);

                relativeLayout = findViewById(R.id.rl_title_red);
                view = findViewById(R.id.liuhai_status_view_red);
                int backgroundColor = attributes.getColor(R.styleable.ActionBarYoung_backColor, 0);
                if (backgroundColor != 0) {
                    relativeLayout.setBackgroundColor(backgroundColor);
                    view.setBackgroundColor(backgroundColor);
                }
            } finally {
                attributes.recycle();
            }
        }
    }

    public void setTitleMenuImg(int originLang) {
        iconFontBack.setImageResource(originLang);
    }

    public void setTitle(String title) {
        tvTitle.setVisibility(VISIBLE);
        tvTitle.setText(title);
    }

    public void setTitle(int resId) {
        tvTitle.setVisibility(VISIBLE);
        tvTitle.setText(resId);
    }

    public void setOnBackClickListener(OnClickListener l) {
        iconFontBack.setOnClickListener(l);
    }

    public void setMenuClick(View.OnClickListener click) {
        fivContactSearch.setOnClickListener(click);
    }

    public void setMenuIcon(@DrawableRes int drawable) {
        fivContactSearch.setImageResource(drawable);
    }

    public void setMenuVisible(boolean flag) {
        if (flag) {
            fivContactSearch.setVisibility(VISIBLE);
        } else {
            fivContactSearch.setVisibility(GONE);
        }
    }

    public void setMenuFunctionClick(OnClickListener click) {
        fontIconLdrtl.setOnClickListener(click);
    }

    public void setMenuFunctionVisible(boolean isVisibleMenuFunction) {
        if (isVisibleMenuFunction) {
            fontIconLdrtl.setVisibility(VISIBLE);
        } else {
            fontIconLdrtl.setVisibility(GONE);
        }
    }

    public void setMenuFunctionText(String isVisibleMenuFunction) {
        fontIconLdrtl.setText(isVisibleMenuFunction);
    }

    public String getMenuFunctionText() {
        return fontIconLdrtl.getText().toString();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }

    public void setEditVisible(boolean isEditVisible) {
        if (isEditVisible) {
            editSearch.setVisibility(VISIBLE);
            ivClose.setVisibility(VISIBLE);
        } else {
            editSearch.setVisibility(GONE);
            ivClose.setVisibility(GONE);
        }
    }

    public void setTitleVisible(boolean isVisibleMenuFunction) {
        if (!isVisibleMenuFunction) {
            tvTitle.setVisibility(VISIBLE);
        } else {
            tvTitle.setVisibility(GONE);
        }
    }

    public String getEditText() {
        return editSearch.getText().toString();
    }

    public interface ClearEdiText {
        void clearEdiText();

        void search();
    }

    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
//        closeKeybord();
        if (actionId == EditorInfo.IME_ACTION_SEARCH || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
//            String searchWord = binding.include.editSearch.getText().toString().trim();
//            map.put("assetName", searchWord);
//            viewModel.getAssestList(false, map);
            clearEdiText.search();
        }
        return false;
    }
}
